/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package admin.controller;

import admin.hibernate.model.dao.AdminChiTietSanPhamDAO;
import admin.hibernate.model.dao.AdminLoaiSanPhamDAO;
import admin.hibernate.model.dao.AdminSanPhamDAO;
import hibernate.model.dao.ChiTietSanPhamDAO;
import hibernate.model.dao.LoaiSanPhamDAO;
import hibernate.model.dao.SanPhamDAO;
import hibernate.pojo.Chitietsanpham;
import hibernate.pojo.Loaisanpham;
import hibernate.pojo.Sanpham;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author Administrator
 */
@WebServlet(name = "admindealdetailcontroller", urlPatterns = {"/admindealdetailcontroller"})
public class admindealdetailcontroller extends HttpServlet {

    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        try 
        {
            request.setCharacterEncoding("UTF-8");
            String action = request.getParameter("action");
            if(action!=null)
            {
                //<editor-fold defaultstate="collapsed" desc="Xem chi tiết sản phẩm(cũng như cập nhật chi tiết sản phẩm)">
                if(action.equals("view"))
                {
                    String masanpham = request.getParameter("masanpham");
                    if(masanpham!=null)
                    {
                        int intMaSanPham = Integer.parseInt(masanpham);
                        Chitietsanpham ctsp = SanPhamDAO.LayChiTietSanPhamHQL(intMaSanPham);
                        if(ctsp!=null)
                        {
                            request.setAttribute("ChiTietSanPhamCanCapNhat", ctsp);
                            request.getRequestDispatcher("Admin/pagestile/updatingdealdetail.jsp").forward(request, response);
                        }
                        else    // Chitietsanpham = null -->Thêm chi tiết sản phẩm cho sản phẩm đó
                        {
                            request.getRequestDispatcher("admindealdetailprocesscontroller?action=linktoadddealdetail&maSanPham="+intMaSanPham).forward(request, response);
                        }
                    }
                }
                //</editor-fold>

                //<editor-fold defaultstate="collapsed" desc="Nhấn vào nút Thêm Chi Tiết Sản Phẩm">
                //NOTE: ADDING WAS NOT COMPLETELY -> FAILED TO VIEW PRODUCT DETAIL
                if(action.equals("adddealdetail"))
                {
                    String strMaSanPham = request.getParameter("maSanPham");
                    int intMaSanPham = 0;
                    if(strMaSanPham!=null)
                    {
                        intMaSanPham = Integer.parseInt(strMaSanPham);
                    }
                    /**
                    String strProductDetailID = request.getParameter("maChiTietSanPham"); 
                    int intProductDetailID = 0;
                    if(!strProductDetailID.equals(""))
                    {
                            intProductDetailID = Integer.parseInt(strProductDetailID);    
                    }
                    */
                    String strTenCongTy = (String) request.getParameter("txtTenCongTy");
                    String strWebsiteCongTy = (String) request.getParameter("txtWebsiteCongTy");
                    String strDiaChiCongTy = (String) request.getParameter("txtDiaChiCongTy");
                    String strDiaChiGoogleMap = (String) request.getParameter("txtDiaChiGoogleMap");
                    String strDiemNoiBat = (String) request.getParameter("txtDiemNoiBat");
                    String strDieuKienApDung = (String) request.getParameter("txtDieuKienApDung");
                    String strThongTinChiTiet = (String) request.getParameter("txtThongTinChiTiet");
                    String strHinhAnhCongTy = request.getParameter("fileHinhAnhCongTy");
                    
                    Chitietsanpham productDetail = new Chitietsanpham();
                    Sanpham product = AdminSanPhamDAO.getProductByID(intMaSanPham); // Lấy sản phẩm ứng với chi tiết sản phẩm đang thêm
                    productDetail.setSanpham(product);  
                    productDetail.setDiemNoiBat(strDiemNoiBat);
                    productDetail.setDieuKienApDung(strDieuKienApDung);
                    productDetail.setTenCongTy(strTenCongTy);
                    productDetail.setWebsiteCongTy(strWebsiteCongTy);
                    productDetail.setDiaChiCongTy(strDiaChiCongTy);
                    productDetail.setDiaChiGoogleMap(strDiaChiGoogleMap);
                    productDetail.setThongTinChiTiet(strThongTinChiTiet);
                    productDetail.setHinhAnhCongTy(strHinhAnhCongTy);
                    
                    boolean addingResult =  AdminChiTietSanPhamDAO.addProductdetail(productDetail) ;
                    if(addingResult==true)
                    {
                        Loaisanpham productType = new Loaisanpham();
                        productType = product.getLoaisanpham();
                        //List<Sanpham> arrSanPham = LoaiSanPhamDAO.LayDanhSachSanPhamTheoLoaiHQL(productType.getTenTrang());
                        List<Sanpham> arrSanPham = AdminLoaiSanPhamDAO.AdminLayDanhSachSanPhamTheoLoaiHQL(productType.getTenTrang());
                        request.setAttribute("DanhSachSanPhamTheoLoai", arrSanPham);
                        request.getRequestDispatcher("Admin/pagestile/listdealbycatalog.jsp").forward(request, response);
                        //request.getRequestDispatcher("admincatalogcontroller?action=listdealbycatalog&catalogtype="+productType.getTenTrang()).forward(request, response);
                    }
                    else
                    {
                        out.print("Thêm không được");
                    }
                }
                //</editor-fold>
                
                //<editor-fold defaultstate="collapsed" desc="Nhấn vào nút Cập Nhật Chi Tiết Sản Phẩm">
                if(action.equals("updatingproductdetail"))
                {
                    String strMaSanPham = request.getParameter("maSanPham");
                    String strProductDetailID = request.getParameter("maChiTietSanPham"); 
                    int intProductDetailID = 0;
                    if(!strProductDetailID.equals(""))
                    {
                            intProductDetailID = Integer.parseInt(strProductDetailID);    
                    }
                    int intMaSanPham = 0;
                    if(!strMaSanPham.equals(""))
                    {
                            intMaSanPham = Integer.parseInt(strMaSanPham);    
                    }
                    Sanpham sanpham = AdminSanPhamDAO.getProductByID(intMaSanPham); // Lấy lại sản phẩm
                    
                    String strTenCongTy = (String) request.getParameter("txtTenCongTy");
                    String strWebsiteCongTy = (String) request.getParameter("txtWebsiteCongTy");
                    String strDiaChiCongTy = (String) request.getParameter("txtDiaChiCongTy");
                    String strDiaChiGoogleMap = (String) request.getParameter("txtDiaChiGoogleMap");
                    String strDiemNoiBat = (String) request.getParameter("txtDiemNoiBat");
                    String strDieuKienApDung = (String) request.getParameter("txtDieuKienApDung");
                    String strThongTinChiTiet = (String) request.getParameter("txtThongTinChiTiet");
                    String strHinhAnhCongTy = request.getParameter("fileHinhAnhCongTy");
                    
                    // Lấy chi tiết sản phẩm đang có theo mã chitietsanpham
                    Chitietsanpham productDetail = AdminChiTietSanPhamDAO.LayChiTietSanPham(intProductDetailID);
                    
                    productDetail.setSanpham(sanpham);
                    productDetail.setDiemNoiBat(strDiemNoiBat);
                    productDetail.setDieuKienApDung(strDieuKienApDung);
                    productDetail.setTenCongTy(strTenCongTy);
                    productDetail.setWebsiteCongTy(strWebsiteCongTy);
                    productDetail.setDiaChiCongTy(strDiaChiCongTy);
                    productDetail.setDiaChiGoogleMap(strDiaChiGoogleMap);
                    productDetail.setThongTinChiTiet(strThongTinChiTiet);
                    productDetail.setHinhAnhCongTy(strHinhAnhCongTy);
                    boolean addingResult =  AdminChiTietSanPhamDAO.CapNhatChiTietSanPham(productDetail) ;
                    if(addingResult==true)
                    {
                        Loaisanpham productType = new Loaisanpham();
                        productType = sanpham.getLoaisanpham();
                        List<Sanpham> arrSanPham = LoaiSanPhamDAO.LayDanhSachSanPhamTheoLoaiHQL(productType.getTenTrang());
                        request.setAttribute("DanhSachSanPhamTheoLoai", arrSanPham);
                        request.getRequestDispatcher("Admin/pagestile/listdealbycatalog.jsp").forward(request, response);
                    }
                    else
                    {
                        out.print("Không cập nhật được chi tiết sản phẩm");
                    }
                }
                //</editor-fold>
            }//end of if
        } finally {            
            out.close();
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
